package com.example.xiaomaotv.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.xiaomaotv.R;
import com.example.xiaomaotv.entity.Msg;

import java.util.List;

public class MsgAdapter extends ArrayAdapter<Msg> {
    private int recouresId;
    public MsgAdapter(@NonNull Context context, int resource, @NonNull List<Msg> objects) {
        super(context, resource, objects);
        recouresId =resource;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Msg msg = getItem(position);//即将展示的项
        View msgView = LayoutInflater.from(getContext()).inflate(recouresId,null);//加载子项布局
        LinearLayout sendLayout = msgView.findViewById(R.id.layout_s);
        LinearLayout recLayout = msgView.findViewById(R.id.layout_r);
        //发送端的两个控件
        TextView sendText =msgView.findViewById(R.id.text_msgs);
        ImageView sendImg =msgView.findViewById(R.id.img_msgs);
        //接收端的两个控件
        TextView recText =msgView.findViewById(R.id.text_msgr);
        ImageView recImg =msgView.findViewById(R.id.img_msgr);
        if (msg.getMsgType() == 0){
            sendLayout.setVisibility(View.VISIBLE);
            recLayout.setVisibility(View.GONE);
            sendText.setText(msg.getContent());
            sendImg.setImageResource(msg.getHeadImg());
        }else {
            recLayout.setVisibility(View.VISIBLE);
            sendLayout.setVisibility(View.GONE);
            recText.setText(msg.getContent());
            recImg.setImageResource(msg.getHeadImg());
        }
        return msgView;
    }
}